import { GetChatJsApiConfig } from "@/uni-sub-base/service/api/web.js"
import { is_wx } from "@/uni-sub-base/utils/platformUtil.js"

// var __originUrl = document.URL
function GetWxJsConfig(callback) {
    // 判断环境是否为微信浏览器
    if (is_wx()) {
        // 没有wxjs则加载wxjs
        var jweixin = require("@/uni-sub-base/plugins/jweixin.js")
        if (window.WeixinJSBridge && WeixinJSBridge.invoke) {
            WeixinJSBridgeReadyCallback(callback)
        } else {
            document.addEventListener(
                "WeixinJSBridgeReady",
                function () {
                    WeixinJSBridgeReadyCallback(callback)
                },
                false,
            )
        }
    }

    function WeixinJSBridgeReadyCallback(callback) {
        var jsApiList = [
            "checkJsApi",
            "onMenuShareTimeline",
            "onMenuShareAppMessage",
            "updateAppMessageShareData",
            "updateTimelineShareData",
            "onMenuShareQQ",
            "onMenuShareWeibo",
            "onMenuShareQZone",
            "hideMenuItems",
            "showMenuItems",
            "hideAllNonBaseMenuItem",
            "showAllNonBaseMenuItem",
            "translateVoice",
            "startRecord",
            "stopRecord",
            "onVoiceRecordEnd",
            "playVoice",
            "onVoicePlayEnd",
            "pauseVoice",
            "stopVoice",
            "uploadVoice",
            "downloadVoice",
            "chooseImage",
            "previewImage",
            "uploadImage",
            "downloadImage",
            "getNetworkType",
            "openLocation",
            "getLocation",
            "hideOptionMenu",
            "showOptionMenu",
            "closeWindow",
            "scanQRCode",
            "chooseWXPay",
            "openProductSpecificView",
            "addCard",
            "chooseCard",
            "openCard",
            "openAddress",
        ]
        GetChatJsApiConfig({
            accountId: 10938,
            url: document.URL,
        }).then((data) => {
            if (!data) {
                return
            }

            var config = data.Data
            jweixin.config({
                debug: false,
                appId: config.AppId,
                timestamp: config.Timestamp,
                nonceStr: config.NonceStr,
                signature: config.Signature,
                jsApiList: jsApiList,
            })
            jweixin.ready(function () {
                if (callback && callback instanceof Function) {
                    callback(jweixin)
                }
            })
            jweixin.error(function (res) {
                // 安卓手机有些浏览器会导致微信url签名失败，这里用原始地址请求一次
                GetChatJsApiConfig({
                    accountId: 10938,
                    url: document.URL,
                }).then((data) => {
                    if (!data) {
                        return
                    }
                    var config = data.data
                    jweixin.config({
                        debug: false,
                        appId: config.AppId,
                        timestamp: config.Timestamp,
                        nonceStr: config.NonceStr,
                        signature: config.Signature,
                        jsApiList: jsApiList,
                    })
                    jweixin.ready(function () {
                        if (callback && callback instanceof Function) {
                            callback(jweixin)
                        }
                    })
                    jweixin.error(function (res) {
                        alert("jweixin error, " + JSON.stringify(res))
                    })
                })
            })
        })
    }
}

export default GetWxJsConfig
